extends Node

# 测试战场伤害结算机制
func _ready():
	print("=== 战场伤害结算机制测试 ===")
	
	# 等待游戏初始化完成
	await get_tree().create_timer(2.0).timeout
	
	test_battlefield_combat_system()

func test_battlefield_combat_system():
	"""测试战场伤害结算系统"""
	print("\n--- 测试1: 基础伤害结算 ---")
	
	# 模拟创建测试单位
	var player_unit1 = create_test_unit("玩家战士", 3, 4)  # 3攻击 4生命
	var player_unit2 = create_test_unit("玩家法师", 2, 3)  # 2攻击 3生命
	var ai_unit1 = create_test_unit("AI兽人", 4, 2)       # 4攻击 2生命
	var ai_unit2 = create_test_unit("AI巨魔", 1, 5)       # 1攻击 5生命
	
	# 添加到战场
	GameManager.player_board = [player_unit1, player_unit2]
	GameManager.ai_board = [ai_unit1, ai_unit2]
	
	print("战场初始状态:")
	print_battlefield_status()
	
	# 执行伤害结算
	print("\n执行战场伤害结算...")
	await GameManager.resolve_battlefield_combat()
	
	print("\n战场结算后状态:")
	print_battlefield_status()
	
	# 测试2: 单方面优势
	print("\n--- 测试2: 单方面优势 ---")
	await test_one_sided_combat()
	
	# 测试3: 空战场
	print("\n--- 测试3: 空战场 ---")
	await test_empty_battlefield()

func create_test_unit(name: String, attack: int, health: int) -> Control:
	"""创建测试单位"""
	var unit = Control.new()
	unit.name = name
	
	# 添加必要的属性
	unit.set_script(preload("res://scripts/Card.gd"))
	unit.card_name = name
	unit.attack = attack
	unit.health = health
	unit.max_health = health
	unit.card_type = "unit"
	
	# 添加必要的方法
	if not unit.has_method("update_card_display"):
		unit.set_script(null)
		unit.set_script(preload("res://scripts/Card.gd"))
	
	return unit

func print_battlefield_status():
	"""打印战场状态"""
	print("玩家战场:")
	for unit in GameManager.player_board:
		if unit and is_instance_valid(unit):
			print("  " + unit.card_name + " - 攻击:" + str(unit.attack) + " 生命:" + str(unit.health))
	
	print("AI战场:")
	for unit in GameManager.ai_board:
		if unit and is_instance_valid(unit):
			print("  " + unit.card_name + " - 攻击:" + str(unit.attack) + " 生命:" + str(unit.health))

func test_one_sided_combat():
	"""测试单方面优势的战斗"""
	# 清空战场
	GameManager.player_board.clear()
	GameManager.ai_board.clear()
	
	# 只有玩家有单位
	var player_unit = create_test_unit("玩家龙", 8, 8)
	GameManager.player_board = [player_unit]
	GameManager.ai_board = []
	
	print("单方面优势测试 - 只有玩家有单位:")
	print_battlefield_status()
	
	await GameManager.resolve_battlefield_combat()
	
	print("结算后:")
	print_battlefield_status()

func test_empty_battlefield():
	"""测试空战场"""
	GameManager.player_board.clear()
	GameManager.ai_board.clear()
	
	print("空战场测试:")
	print_battlefield_status()
	
	await GameManager.resolve_battlefield_combat()
	
	print("结算后:")
	print_battlefield_status()